package mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.anye.greendao.gen.InfoBeanDao;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import mvp.com.bbc.rcjavaretrofitdaggermvp.R;
import mvp.com.bbc.rcjavaretrofitdaggermvp.app.BaseApplication;
import mvp.com.bbc.rcjavaretrofitdaggermvp.app.contact.InfoContact;
import mvp.com.bbc.rcjavaretrofitdaggermvp.app.utils.NetWorkStateUtil;
import mvp.com.bbc.rcjavaretrofitdaggermvp.baseAF.BaseFragment;
import mvp.com.bbc.rcjavaretrofitdaggermvp.bean.InfoBean;
import mvp.com.bbc.rcjavaretrofitdaggermvp.di.component.DaggerInfoPresenterCompnonet;
import mvp.com.bbc.rcjavaretrofitdaggermvp.di.modules.InfoPresenterModules;
import mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.adapter.InfoAdapter;
import mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.utils.InfoBeanUtils;
import mvp.com.bbc.rcjavaretrofitdaggermvp.presenter.InfoPresenter;
import tech.michaelx.loadinglibrary.LoadingLayout;

/**
 * Created by ${cbf} on 2017/11/24.
 */

public class HomeFragment extends BaseFragment implements InfoContact.InfoView {
    @BindView(R.id.rv_base_recyclerview)
    RecyclerView RV;
    @BindView(R.id.smartrefresh)
    SmartRefreshLayout smartrefresh;
    @BindView(R.id.loadinglayout)
    LoadingLayout loadinglayout;
    private InfoPresenter presenter;
    private String type = "1";
    private int page = 1;
    private int limit = 12;
    private InfoAdapter adapter;
    private List<InfoBean> list;
    private InfoBeanDao dao;

    @Override
    protected int setContentLayout() {
        return R.layout.base_refresh_loading;
    }

    @Override
    protected void findViews() {

    }

    @Override
    protected void init() {
        type = getArguments().getString("type");
        RV.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new InfoAdapter(getContext());
        RV.setAdapter(adapter);
        presenter = DaggerInfoPresenterCompnonet
                .builder()
                .infoPresenterModules(new InfoPresenterModules(this))
                .build()
                .getPresenter();
        if (type.equals("4")) {
            presenter.InfoMyQAPresenter(type,String.valueOf(page),String.valueOf(limit),"20");
        } else {
            presenter.infoPresenter(type, String.valueOf(page), String.valueOf(limit));
        }
        presenter.infoPresenter(type, String.valueOf(page), String.valueOf(limit));
        //这里做的是本地缓存 这样在无网的状态下可以进行此次操作
        dao = BaseApplication.getInstances().getDaoSession().getInfoBeanDao();
        list = dao.queryBuilder().list();
    }

    @Override
    public void infoView(List<InfoBean> infoBeen) {
        if (infoBeen == null) {
            loadinglayout.showEmpty();
        } else {
            if (page == 1) {
                adapter.setData(infoBeen);
            } else {
                adapter.addData(infoBeen);
            }
            loadinglayout.loadComplete();
        }

    }

    /***
     * 没有网络时从本地缓存中获取数据
     */
    @Override
    public void onEvent(String action) {
        super.onEvent(action);
        if (action.equals("网络请求失败")) {
            adapter.setData(list);
            loadinglayout.loadComplete();
        }
    }

    @Override
    protected void setListeners() {
        smartrefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                if (type.equals("4")) {
                    presenter.InfoMyQAPresenter(type,String.valueOf(page),String.valueOf(limit),"20");
                } else {
                    presenter.infoPresenter(type, String.valueOf(page), String.valueOf(limit));
                }
                refreshlayout.finishRefresh(1000);
            }
        });
        smartrefresh.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                if (type.equals("4")) {
                    presenter.InfoMyQAPresenter(type,String.valueOf(page),String.valueOf(limit),"20");
                } else {
                    presenter.infoPresenter(type, String.valueOf(page), String.valueOf(limit));
                }
                refreshlayout.finishLoadmore(1000);
            }
        });

    }

    @Override
    public void onClick(View view) {

    }

    public static Fragment Instance(String type) {
        HomeFragment homeFragment = new HomeFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        homeFragment.setArguments(bundle);
        return homeFragment;
    }

}
